package com.dqhl.community.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.dqhl.community.R;
import com.dqhl.community.base.BaseActivity;
import com.dqhl.community.util.Config;
import com.dqhl.community.util.Dlog;
import com.dqhl.community.util.JsonUtils;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * Created by cheng on 2016/8/30.
 * 修改密码
 */
public class ModifyPwdActivity extends BaseActivity implements View.OnClickListener {

    private ImageView ivTopBack;
    private TextView tvTopTitle;
    private EditText etOldPwd; // 旧密码输入框
    private EditText etNewPwd; // 新密码输入框
    private EditText etConfirmPwd; // 确认密码输入框
    private Button btnConfirm; // 确定

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify_pwd);

        initView();
    }

    /**
     * 初始化控件
     */
    private void initView() {
        ivTopBack = (ImageView) findViewById(R.id.iv_top_back);
        ivTopBack.setVisibility(View.VISIBLE);
        tvTopTitle = (TextView) findViewById(R.id.tv_top_title);
        tvTopTitle.setText(R.string.txt_modify_password);
        etOldPwd = (EditText) findViewById(R.id.et_old_pwd);
        etNewPwd = (EditText) findViewById(R.id.et_new_pwd);
        etConfirmPwd = (EditText) findViewById(R.id.et_confirm_pwd);
        btnConfirm = (Button) findViewById(R.id.btn_confirm);

        ivTopBack.setOnClickListener(this);
        btnConfirm.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_top_back:
                finish();
                break;
            case R.id.btn_confirm:
                doConfirm();
                break;
            default:
                break;
        }
    }

    /**
     * 确认提交
     */
    private void doConfirm() {
        String OldPwd =  etOldPwd.getText().toString();
        String NewPwd = etNewPwd.getText().toString();
        String ConfirmPwd = etConfirmPwd.getText().toString();
        if("".equals(OldPwd)){
            toast("旧密码不能为空");
        }else if("".equals(NewPwd)){
            toast("新密码不能为空");
        }else if("".equals(ConfirmPwd)){
            toast("确认密码不能为空");
        }else if(!NewPwd.equals(ConfirmPwd)){
            toast("密码不一致");
        }else{
            RequestParams params = new RequestParams(Config.Api.modifyPasswrod);
            params.addBodyParameter("password_new",NewPwd);
            params.addBodyParameter("password_old",OldPwd);
            params.addBodyParameter("userid",app.getUserId());
            Dlog.e(NewPwd+" NewPwd "+OldPwd+" OldPwd "+app.getUserId());
            x.http().post(params, new Callback.CommonCallback<String>() {
                @Override
                public void onSuccess(String result) {
                    int errCode = JsonUtils.getErrCode(result);
                    String errMAsg = JsonUtils.getInfo(result,"errMsg");
                    Dlog.e(errCode+"GG修改密码"+result);
                    if(errCode == 0){
                        toast("密码改成功");
                        finish();
                    }else if(errCode == 4017){
                        toast(errMAsg);
                    }else if(errCode == 4004){
                        toast("密码为6-12为的数字、字母、下划线");
                    }
                }

                @Override
                public void onError(Throwable ex, boolean isOnCallback) {

                }

                @Override
                public void onCancelled(CancelledException cex) {

                }

                @Override
                public void onFinished() {

                }
            });
        }
    }
}
